package org.karelun.gef.figure;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

public abstract class NodeFigure extends Figure{

	protected Label label;

	private RGB bgColor;

	private RGB foreColor;

	private FontData fontData;

	public NodeFigure() {
		super();
	}

	public void setText(String name) {
		label.setText(name);
	}

	public void setBGColor(RGB bgColor) {
		if (bgColor == null)
			return;
		this.bgColor = bgColor;
		setBGColor(toColor(bgColor));
	}
	
	protected abstract void setBGColor(Color color);
	
	
	public void setForeColor(RGB foreColor) {
		if (foreColor == null)
			return;
		this.foreColor = foreColor;
		label.setForegroundColor(toColor(foreColor));
	}

	public void setFontData(FontData fontData) {
		if (fontData == null)
			return;
		this.fontData = fontData;
		label.setFont(toFont(fontData));
	}

	public String getText() {
		return label.getText();
	}

	public Rectangle getTextBounds() {
		return label.getTextBounds();
	}

	public Color getBGColor() {
		return toColor(bgColor);
	}

	public Color getForeColor() {
		return toColor(foreColor);
	}

	public Font getTextFont() {
		return toFont(fontData);
	}

	private Color toColor(RGB rgb) {
		if (rgb == null)
			return null;
		return new Color(Display.getCurrent(), rgb);
	}

	private Font toFont(FontData fontData) {
		if (fontData == null)
			return null;
		return new Font(Display.getCurrent(), fontData);
	}

	public void setIcon(Image icon) {
		label.setIcon(icon);
	}
}
